<template>
  <el-dialog title="采购单合并" width="40%" :close-on-click-modal="false" draggable v-model="visible">
    <el-form ref="dataFormRef" :model="dataForm" @keyup.enter="dataFormSubmit(dataFormRef)" label-width="80px">
      <el-form-item label="采购单" prop="assigneeId">
        <el-select v-model="dataForm.purchaseId" filterable remote clearable :loading="purchase.loading" placeholder="请输入采购单" style="width: 100%">
          <el-option v-for="item in purchase.dataList" :key="item.id" :label="item.username" :value="item.id">
            <el-row :gutter="20">
              <el-col :span="6">{{ item.id }}</el-col>
              <el-col :span="18">
                <span class="fright">{{ item.username }}</span>
              </el-col>
            </el-row>
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import { ElMessageBox, FormInstance } from "element-plus";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";

// 弹窗是否显示
const visible = ref(false);

// 提交状态
const submitted = ref(false);

// 采购单
const purchase = reactive({
  // 列表加载状态
  loading: false,
  // 列表数据
  dataList: [],
});

// 表单数据
const dataForm = reactive({
  // 采购单
  purchaseId: undefined,
  // 仓库ID
  wareId: undefined,
  // 采购需求ID数组
  purchasedetailIds: undefined,
});

// 表单引用
const dataFormRef = ref<FormInstance>();

// 父级组件的自定义事件
const emit = defineEmits(['loadTableData']);

// 初始化表单
const initForm = (d) => {
  // 表单数据初始化
  dataForm.wareId = d.wareId;
  dataForm.purchasedetailIds = d.purchasedetailIds;
  visible.value = true;
  // 采购单列表初始化
  purchase.loading = true;
  http
    .post({
      url: `/ware/purchase/unassigned-list?wareId=${d.wareId}`,
    }).then(({ data }) => {
      purchase.dataList = http.success(data) ? data.body : [];
      purchase.loading = false;
    });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  // 选择采购单处理
  if (dataForm.purchaseId) {
    combineHandleEvent(form);
    return;
  }
  // 未选择采购单处理
  ElMessageBox.confirm(`没有选择任何【采购单】，将自动创建新单进行合并，请确认是否操作?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    combineHandleEvent(form);
  });
};

// 合并处理
const combineHandleEvent = (form: FormInstance | undefined) => {
  form.validate((valid) => {
    if (valid) {
      submitted.value = true;
      http
        .post({
          url: "/ware/purchase-detail/combine",
          data: dataForm,
        })
        .then(({ data }) => {
          if (http.success(data, true, true)) {
            visible.value = false;
            emit("loadTableData");
            return;
          }
          submitted.value = false;
        });
    }
  });
};

// 暴露方法
defineExpose({ initForm });
</script>
